package frontend.admin;

import backend.DiseaseItem;
import javax.swing.*;
import java.awt.*;

public class DataDialog extends JDialog {
    public interface DataCallback {
        void onDataUpdated(Object item);
    }

    private final String type;
    private final Object existingItem;
    private final DataCallback callback;
    private final JPanel formPanel;

    public DataDialog(JFrame owner, String type, Object existingItem, DataCallback callback) {
        super(owner, type.equals("user") ?
                (existingItem == null ? "添加用户" : "修改用户") :
                (existingItem == null ? "添加疾病" : "修改疾病"), true);

        this.type = type;
        this.existingItem = existingItem;
        this.callback = callback;

        setSize(500, type.equals("user") ? 300 : 400);
        setLocationRelativeTo(owner);
        setLayout(new BorderLayout(10, 10));

        formPanel = new JPanel(new GridLayout(
                type.equals("user") ? 3 : 9, 2, 10, 10));
        formPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 10, 15));

        if (type.equals("user")) {
            setupUserForm();
        } else {
            setupDiseaseForm();
        }

        add(formPanel, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        JButton saveBtn = new JButton("保存");
        JButton cancelBtn = new JButton("取消");

        saveBtn.setBackground(new Color(70, 130, 180));
        saveBtn.setForeground(Color.WHITE);
        saveBtn.setFocusPainted(false);

        saveBtn.addActionListener(e -> handleSave());
        cancelBtn.addActionListener(e -> dispose());

        buttonPanel.add(saveBtn);
        buttonPanel.add(cancelBtn);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void setupUserForm() {
        JTextField usernameField = new JTextField();
        JTextField passwordField = new JTextField();
        JTextField permissionField = new JTextField();

        if (existingItem != null) {
            DataManagementPanel.UserData user = (DataManagementPanel.UserData) existingItem;
            usernameField.setText(user.username);
            passwordField.setText(user.password);
            permissionField.setText(user.permission);
        }

        formPanel.add(new JLabel("用户名："));
        formPanel.add(usernameField);
        formPanel.add(new JLabel("密码："));
        formPanel.add(passwordField);
        formPanel.add(new JLabel("权限："));
        formPanel.add(permissionField);
    }

    private void setupDiseaseForm() {
        JTextField nameField = new JTextField();
        JTextField symptomField = new JTextField();
        JTextField organField = new JTextField();
        JTextField medicationField = new JTextField();
        JTextField reliefMethodField = new JTextField();
        JTextField dietField = new JTextField();
        JTextField impactField = new JTextField();
        JTextField urgencyField = new JTextField();
        JTextField departmentField = new JTextField();

        if (existingItem != null) {
            DataManagementPanel.DiseaseData disease = (DataManagementPanel.DiseaseData) existingItem;
            nameField.setText(disease.name);
            symptomField.setText(disease.symptom);
            departmentField.setText(disease.department);
        }

        formPanel.add(new JLabel("疾病名称："));
        formPanel.add(nameField);
        formPanel.add(new JLabel("症状："));
        formPanel.add(symptomField);
        formPanel.add(new JLabel("发生部位："));
        formPanel.add(organField);
        formPanel.add(new JLabel("药物治疗："));
        formPanel.add(medicationField);
        formPanel.add(new JLabel("缓解方式："));
        formPanel.add(reliefMethodField);
        formPanel.add(new JLabel("饮食建议："));
        formPanel.add(dietField);
        formPanel.add(new JLabel("影响程度："));
        formPanel.add(impactField);
        formPanel.add(new JLabel("紧急程度："));
        formPanel.add(urgencyField);
        formPanel.add(new JLabel("科室："));
        formPanel.add(departmentField);
    }

    private void handleSave() {
        // Get all field values and create appropriate object
        if (type.equals("user")) {
            // Handle user save
            JTextField usernameField = (JTextField) formPanel.getComponent(1);
            JTextField passwordField = (JTextField) formPanel.getComponent(3);
            JTextField permissionField = (JTextField) formPanel.getComponent(5);

            String username = usernameField.getText().trim();
            String password = passwordField.getText().trim();
            String permission = permissionField.getText().trim();

            if (!username.isEmpty() && !password.isEmpty() && !permission.isEmpty()) {
                callback.onDataUpdated(new DataManagementPanel.UserData(username, password, permission));
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "请填写所有字段");
            }
        } else {
            // Handle disease save
            JTextField nameField = (JTextField) formPanel.getComponent(1);
            JTextField symptomField = (JTextField) formPanel.getComponent(3);
            JTextField organField = (JTextField) formPanel.getComponent(5);
            JTextField medicationField = (JTextField) formPanel.getComponent(7);
            JTextField reliefMethodField = (JTextField) formPanel.getComponent(9);
            JTextField dietField = (JTextField) formPanel.getComponent(11);
            JTextField impactField = (JTextField) formPanel.getComponent(13);
            JTextField urgencyField = (JTextField) formPanel.getComponent(15);
            JTextField departmentField = (JTextField) formPanel.getComponent(17);

            String name = nameField.getText().trim();
            String symptom = symptomField.getText().trim();
            String organ = organField.getText().trim();
            String medication = medicationField.getText().trim();
            String reliefMethod = reliefMethodField.getText().trim();
            String diet = dietField.getText().trim();
            String impact = impactField.getText().trim();
            String urgency = urgencyField.getText().trim();
            String department = departmentField.getText().trim();

            if (!name.isEmpty() && !symptom.isEmpty() && !organ.isEmpty() &&
                    !medication.isEmpty() && !reliefMethod.isEmpty() && !diet.isEmpty() &&
                    !impact.isEmpty() && !urgency.isEmpty() && !department.isEmpty()) {

                DiseaseItem disease = new DiseaseItem()
                        .Cause(name)
                        .Symptom(symptom)
                        .Organ(organ)
                        .Medication(medication)
                        .Reliefmethod(reliefMethod)
                        .Diet(diet)
                        .Impact(impact)
                        .Urgency(urgency)
                        .Department(department);

                callback.onDataUpdated(disease);
                dispose();
            } else {
                JOptionPane.showMessageDialog(this, "请填写所有字段");
            }
        }
    }
}